package com.gsafety.libraryktx.net.ssl

import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.*

/**
 * @author : lj
 * @date : 2021/1/11 10:02
 */
object SSLSocketClient {
    fun SSLSocketFactory(): SSLSocketFactory {
        return try {
            val sslContext = SSLContext.getInstance("SSL")
            sslContext.init(
                null,
                getTrustManager(),
                SecureRandom()
            )
            sslContext.socketFactory
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }

    /**
     * 获取TrustManager
     */
     fun getTrustManager(): Array<TrustManager>? {
        return arrayOf(object : X509TrustManager {
            override fun checkClientTrusted(
                chain: Array<X509Certificate>,
                authType: String
            ) {
            }

            override fun checkServerTrusted(
                chain: Array<X509Certificate>,
                authType: String
            ) {
            }

            override fun getAcceptedIssuers(): Array<X509Certificate> {
                return arrayOf()
            }
        })
    }

    //获取HostnameVerifier
    fun getHostnameVerifier(): HostnameVerifier{
        return HostnameVerifier { s, sslSession -> true }
    }
}